﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XamarinToolKit.Model;

namespace XamarinToolKit.Interfaces
{
    public class ToolKitService : IToolKitService
    {
        public async Task<bool> AddToolKitDetail(ToolKitDetail detail)
        {
            App.Instance.ToolKitDetails.Add(detail);

            return await App.Instance.SaveChangesAsync() > 0;
        }

        public async Task<bool> DeleteToolKitDetail(int id)
        {
            var detail = await App.Instance.ToolKitDetails.FirstOrDefaultAsync(d => Equals(d.Id, id));
            if (detail != null)
            {
                App.Instance.ToolKitDetails.Remove(detail);
                return await App.Instance.SaveChangesAsync() > 0;
            }
            return false;
        }

        public async Task<List<ToolKitDetail>> GetToolKitDetailsAsync(int masterId)
        {
            return await App.Instance.ToolKitDetails.Where(d => d.MasterId == masterId).ToListAsync();
        }

        public async Task<ToolKitMaster> GetToolKitMasterByIdAsync(int id)
        {
            return await App.Instance.ToolKitMasters.FirstOrDefaultAsync(d => d.Id == id);
        }

        public async Task<List<ToolKitMaster>> GetToolKitMastersAsync()
        {
            return await App.Instance.ToolKitMasters.ToListAsync();
        }

        public async Task<bool> UpdateToolKitDetail(ToolKitDetail detail)
        {
            var model = await App.Instance.ToolKitDetails.FirstOrDefaultAsync(d => d.Id == detail.Id);
            if (model != null)
            {
                model.Name = detail.Name;
                model.Account = detail.Account;
                model.PassWord = detail.PassWord;
            }

            return await App.Instance.SaveChangesAsync() > 0;
        }
    }
}
